package com.huiquan.foundation.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import com.huiquan.foundation.constant.BusinessContants;
import com.huiquan.framework.utils.FileUtils;
import com.huiquan.framework.utils.ShellUtils;

public class BusinessUtil {

	public static String[] getSynonymyArray(String s) {
		String[] array = new String[1];
		if (s != null && !s.isEmpty()) {
			String[] split = s.trim().split("\t");
			array = new String[split.length + 1];
			System.arraycopy(split, 0, array, 0, split.length);
			array[split.length] = "";
		} else {
			array[0] = "";
		}
		return array;
	}

	public static String systemEnvSign;

	public static boolean isProductionEnvironment() {
		if (systemEnvSign == null) {
			try {
				String resourcePath = BusinessUtil.class.getClassLoader().getResource("/").getPath();

				BufferedReader read = new BufferedReader(
						new FileReader(new File(resourcePath + "config/config.properties")));
				String sign = read.readLine();
				read.close();
				return sign.indexOf(BusinessContants.SYSTEM_EVN_SIGN_PRODUCTION) > -1;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		return BusinessContants.SYSTEM_EVN_SIGN_PRODUCTION.equals(systemEnvSign);
	}

	public static boolean isTestEnvironment() {
		if (systemEnvSign == null) {
			try {
				String resourcePath = BusinessUtil.class.getClassLoader().getResource("/").getPath();

				BufferedReader read = new BufferedReader(
						new FileReader(new File(resourcePath + "config/config.properties")));
				String sign = read.readLine();
				read.close();
				return sign.indexOf(BusinessContants.SYSTEM_EVN_SIGN_TEST) > -1;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		return BusinessContants.SYSTEM_EVN_SIGN_TEST.equals(systemEnvSign);
	}

	/**
	 * 
	 * @param vocab
	 * @param prop
	 * @return
	 */
	public static String getVocabAndPropFormat(String vocab, String prop) {
		return "[" + vocab + "(" + prop + ")]";
	}

	/**
	 * 执行shell脚本
	 * 
	 * @param args
	 * @return
	 * @throws IOException
	 */
	public static String excuteShell(String[] args) throws IOException {

		if (BusinessUtil.isProductionEnvironment() || BusinessUtil.isTestEnvironment()) {
			String shArgs[] = new String[args.length + 1];
			shArgs[0] = "sh";
			// 给执行脚本增加文件路径
			shArgs[1] = addShellFolderPath(args[0]);
			if (args.length > 1) {
				for (int i = 1; i < args.length; i++) {
					shArgs[i + 1] = args[i].replace("(", "\\(").replace(")", "\\)");
				}
			}

			return ShellUtils.executeShell(shArgs);
		} else {
			return "Development environment";
		}
	}

	public static String addShellFolderPath(String fileName) {

		String resourcePath = BusinessUtil.class.getClassLoader().getResource("/").getPath();

		return resourcePath + "../../shell/" + fileName;
	}

	public static String getCompletePath4Properties(String fileName) {
		if (fileName != null && !fileName.isEmpty()) {
			if (fileName.startsWith("/")) {
				return fileName;
			} else {
				return addShellFolderPath(fileName);
			}
		} else {
			throw new RuntimeException("Wrong Param!");
		}
	}

	public static File createFileByProperties(String filePath) throws Exception {

		return FileUtils.createFile(BusinessUtil.getCompletePath4Properties(filePath));
	}

	public static String getStringFromListBySeparator(List<String> list, String separator) {
		StringBuilder sb = new StringBuilder(separator);
		for (String s : list) {
			sb.append(s);
			sb.append(separator);
		}

		return sb.toString();
	}
}
